﻿/*
 * 2012  - C.B.S. - Compute Software Solutions
 * This file is part of Observation Log.
 * 
 *     Observation Log is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Observation Log is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Observation Log.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Data;
using System.Windows.Forms;

namespace ObservationLog
{
    public partial class frmInstruments : Form
    {
        public frmInstruments()
        {
            InitializeComponent();
        }

        private void frmInstruments_Load(object sender, EventArgs e)
        {
            this.instrumentsTableAdapter.Fill(this.observationLogDBDataSet.Instruments);
        }

        private void frmInstruments_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Save changes upon closing the form 
            Save();
            this.instrumentsBindingSource.ResetCurrentItem();
            this.DialogResult = this.instrumentsBindingSource.Current != null ? 
                DialogResult.OK : DialogResult.Cancel;
        }

        private void Save()
        {
            int sRow = this.instrumentsDataGridView.SelectedRows.Count == 1 ? 
                this.instrumentsDataGridView.SelectedRows[0].Index : 0;
            this.Validate();
            this.instrumentsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.observationLogDBDataSet);
            this.instrumentsTableAdapter.Fill(this.observationLogDBDataSet.Instruments);
            this.instrumentsBindingSource.Position = sRow;
        }
        /// <summary>
        /// Gets the InstrumentId for the currently selected row
        /// </summary>
        public int InstrumentId
        {
            get
            {
                int intsrumentId = 0;
                if (instrumentsBindingSource.Current != null)
                {
                    var drv = (DataRowView)this.instrumentsBindingSource.Current;
                    intsrumentId = Convert.ToInt32((drv)["InstrumentId"]);
                }
                return intsrumentId;
            }
        }


    }
}
